package com.yzh.openai.domain.tool.service;

import com.yzh.zhipu.model.ChatRequest;
import com.yzh.zhipu.model.args.Prompt;
import com.yzh.zhipu.model.vo.Model;
import com.yzh.zhipu.model.vo.Role;
import com.yzh.zhipu.session.ChatGlmSession;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public abstract class AbstractToolService implements IToolService{
    @Resource
    protected ChatGlmSession glmSession;

    /**
     * 生成AI分析
     * @param rule      规则（要AI做什么）
     * @param content   内容（用户传入的要分析的数据）
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    protected String genAiAnalysis(String rule, String content) throws ExecutionException, InterruptedException {
        ChatRequest chatRequest = new ChatRequest();
        chatRequest.setModel(Model.GLM_3_TURBO);
        chatRequest.setMessages(new ArrayList<Prompt>() {

            {
                add(Prompt.builder()
                        .role(Role.user.getCode())
                        .content(rule)
                        .build());
            }

            {
                add(Prompt.builder()
                        .role(Role.user.getCode())
                        .content(content)
                        .build());
            }
        });
        CompletableFuture completions = glmSession.completions(chatRequest);
        return (String) completions.get();
    }
}
